// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt

frappe.ui.form.on('SRM Instock Return', {
	refresh: function(frm) {
		frm.add_custom_button(__("SRM Delivery Checking"), function() {
			const current_doc = $('.data-row.editable-row').parent().attr("data-name");
			const item_row = locals["SRM Instock Return Sub"][current_doc];
			if (item_row.receive_qty > item_row.check_qty) {
				frappe.db.get_list('SRM Purchase Delivery Sub', {
					fields: ['name', 'parent', 'creation', 'real_qty'],
					filters: {
						po_entryid: item_row.erp_po_entryid,
						// remain_qty: ['>', 0],
					}
				}).then((r) => {
					if (r.length > 0){
						const d = new frappe.ui.Dialog({
							title: __('核销验退数量'),
							width: 400
						});
						$(`<div class="modal-body ui-front">
							<h2>物料:${item_row.material_number} 验退数量:${item_row.receive_qty}</h2>
							<table class="table table-bordered">
							<thead>
								<tr>
								<th>选择</th>
								<th>单据号</th>
								<th>日期</th>
								<th>数量</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
							</table>
						</div>`).appendTo(d.body);
						r.forEach(element => {
							const tbody = $(d.body).find('tbody');
							const tr = $(`
							<tr>
								<td><input type="checkbox" class="check-delivery" data-row-name="${element.name}"></td>
								<td>${element.parent}</td>
								<td>${element.creation}</td>
								<td>${element.real_qty }</td>
							</tr>
							`).appendTo(tbody)
							tbody.find('.check-delivery').on('change', function() {
								$('input.data-row-name').not(this).prop('checked', false);  
							});
						});
						d.set_primary_action(__("核销"), function() {
							$(d.body).find('input:checked').each(function(i, input) {
								console.log($(input).attr('data-row-name'));
								console.log(frm.doc.srm_supplier);
								console.log(item_row.material_number, item_row.uom, item_row.receive_qty)
								frappe.call({
									method: 'srm.srm.doctype.srm_delivery_checking.srm_delivery_checking.relation_create',
									args: {
										supplier: frm.doc.srm_supplier, 
										po_entryid: item_row.erp_po_entryid, 
										srm_return_name: item_row.name,
										delivery_sub_name: $(input).attr('data-row-name'), 
										material_number: item_row.material_number,
										return_qty: item_row.receive_qty,
										po_billno: item_row.po,
										uom: item_row.uom,
									},
										// disable the button until the request is completed
									btn: $('.primary-action'),
									// freeze the screen until the request is completed
									freeze: true,
									callback: (r) => {
										// on success
										console.log(r);
										frm.refresh();
									},
									error: (r) => {
										// on error
										console.log(r)
									}
								})
							// frappe.model.set_value(item_row.doctype, item_row.name, 'warehouse', $(input).attr('data-row-name'));
							});
							cur_frm.rec_dialog.hide();
							cur_frm.refresh();
						});
						cur_frm.rec_dialog = d;
						d.show();  
					}
					else(frappe.msgprint(__('No Matching Record Lines')))
				})
			}
			else (frappe.msgprint('已经核销完成'))
		})
	}
});
